home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Game Programming Gurus / Tricks of the Windows Game Programming Gurus (SAMS)(2000).iso / Goodies / t3dconsole2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-31  |  9.5 KB  |  348 lines

  1. // T3DCONSOLE2.CPP - First template for Tricks 3D Vol II
  2. // Use this as a template for your applications if you wish
  3. // you may want to change things like the resolution of the
  4. // application, if it's windowed, the directinput devices
  5. // that are acquired and so forth...
  6. // currently the app creates a 640x480x16 windowed display
  7. // hence, you must be in 16 bit color before running the application
  8. // if you want fullscreen mode then simple change the WINDOWED_APP
  9. // value in the #defines belowe value to FALSE (0). Similarly, if
  10. // you want another bitdepth, maybe 8-bit for 256 colors then 
  11. // change that in the call to DDraw_Init() in the function
  12. // Game_Init() within this file.
  13.  
  14. // READ THIS!
  15. // To compile make sure to include DDRAW.LIB, DSOUND.LIB,
  16. // DINPUT.LIB, WINMM.LIB in the project link list, and of course 
  17. // the C++ source modules T3DLIB1.CPP,T3DLIB2.CPP, and T3DLIB3.CPP
  18.  
  19. // INCLUDES ///////////////////////////////////////////////
  20.  
  21. #define INITGUID       // make sure al the COM interfaces are available
  22.                        // instead of this you can include the .LIB file
  23.                        // DXGUID.LIB
  24.  
  25. #define WIN32_LEAN_AND_MEAN  
  26.  
  27. #include <windows.h>   // include important windows stuff
  28. #include <windowsx.h> 
  29. #include <mmsystem.h>
  30. #include <iostream.h> // include important C/C++ stuff
  31. #include <conio.h>
  32. #include <stdlib.h>
  33. #include <malloc.h>
  34. #include <memory.h>
  35. #include <string.h>
  36. #include <stdarg.h>
  37. #include <stdio.h> 
  38. #include <math.h>
  39. #include <io.h>
  40. #include <fcntl.h>
  41.  
  42. #include <ddraw.h>  // directX includes
  43. #include <dsound.h>
  44. #include <dmksctrl.h>
  45. #include <dmusici.h>
  46. #include <dmusicc.h>
  47. #include <dmusicf.h>
  48. #include <dinput.h>
  49. #include "T3DLIB1.h" // game library includes
  50. #include "T3DLIB2.h"
  51. #include "T3DLIB3.h"
  52.  
  53. // DEFINES ////////////////////////////////////////////////
  54.  
  55. // defines for windows interface
  56. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  57. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  58. #define WINDOW_WIDTH      640   // size of window
  59. #define WINDOW_HEIGHT     480
  60.  
  61. #define WINDOW_BPP        16    // bitdepth of window (8,16,24 etc.)
  62.                                 // note: if windowed and not
  63.                                 // fullscreen then bitdepth must
  64.                                 // be same as system bitdepth
  65.                                 // also if 8-bit the a pallete
  66.                                 // is created and attached
  67.  
  68. #define WINDOWED_APP      1     // 0 not windowed, 1 windowed
  69.  
  70. // PROTOTYPES /////////////////////////////////////////////
  71.  
  72. // game console
  73. int Game_Init(void *parms=NULL);
  74. int Game_Shutdown(void *parms=NULL);
  75. int Game_Main(void *parms=NULL);
  76.  
  77. // GLOBALS ////////////////////////////////////////////////
  78.  
  79. HWND main_window_handle           = NULL; // save the window handle
  80. HINSTANCE main_instance           = NULL; // save the instance
  81. char buffer[256];                          // used to print text
  82.  
  83. // FUNCTIONS //////////////////////////////////////////////
  84.  
  85. LRESULT CALLBACK WindowProc(HWND hwnd, 
  86.                             UINT msg, 
  87.                             WPARAM wparam, 
  88.                             LPARAM lparam)
  89. {
  90. // this is the main message handler of the system
  91. PAINTSTRUCT    ps;           // used in WM_PAINT
  92. HDC            hdc;       // handle to a device context
  93.  
  94. // what is the message 
  95. switch(msg)
  96.     {    
  97.     case WM_CREATE: 
  98.         {
  99.         // do initialization stuff here
  100.         return(0);
  101.         } break;
  102.  
  103.     case WM_PAINT:
  104.          {
  105.          // start painting
  106.          hdc = BeginPaint(hwnd,&ps);
  107.  
  108.          // end painting
  109.          EndPaint(hwnd,&ps);
  110.          return(0);
  111.         } break;
  112.  
  113.     case WM_DESTROY: 
  114.         {
  115.         // kill the application            
  116.         PostQuitMessage(0);
  117.         return(0);
  118.         } break;
  119.  
  120.     default:break;
  121.  
  122.     } // end switch
  123.  
  124. // process any messages that we didn't take care of 
  125. return (DefWindowProc(hwnd, msg, wparam, lparam));
  126.  
  127. } // end WinProc
  128.  
  129. // WINMAIN ////////////////////////////////////////////////
  130.  
  131. int WINAPI WinMain(    HINSTANCE hinstance,
  132.                     HINSTANCE hprevinstance,
  133.                     LPSTR lpcmdline,
  134.                     int ncmdshow)
  135. {
  136. // this is the winmain function
  137.  
  138. WNDCLASS winclass;    // this will hold the class we create
  139. HWND     hwnd;        // generic window handle
  140. MSG         msg;        // generic message
  141. HDC      hdc;       // generic dc
  142. PAINTSTRUCT ps;     // generic paintstruct
  143.  
  144. // first fill in the window class stucture
  145. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  146.                           CS_HREDRAW | CS_VREDRAW;
  147. winclass.lpfnWndProc    = WindowProc;
  148. winclass.cbClsExtra        = 0;
  149. winclass.cbWndExtra        = 0;
  150. winclass.hInstance        = hinstance;
  151. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  152. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  153. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  154. winclass.lpszMenuName    = NULL; 
  155. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  156.  
  157. // register the window class
  158. if (!RegisterClass(&winclass))
  159.     return(0);
  160.  
  161. // create the window, note the test to see if WINDOWED_APP is
  162. // true to select the appropriate window flags
  163. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  164.                           WINDOW_TITLE,     // title
  165.                           (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU) : (WS_POPUP | WS_VISIBLE)),
  166.                            0,0,       // x,y
  167.                           WINDOW_WIDTH,  // width
  168.                           WINDOW_HEIGHT, // height
  169.                           NULL,       // handle to parent 
  170.                           NULL,       // handle to menu
  171.                           hinstance,// instance
  172.                           NULL)))    // creation parms
  173. return(0);
  174.  
  175. // save the window handle and instance in a global
  176. main_window_handle = hwnd;
  177. main_instance      = hinstance;
  178.  
  179. // resize the window so that client is really width x height
  180. if (WINDOWED_APP)
  181. {
  182. // now resize the window, so the client area is the actual size requested
  183. // since there may be borders and controls if this is going to be a windowed app
  184. // if the app is not windowed then it won't matter
  185. RECT window_rect = {0,0,WINDOW_WIDTH,WINDOW_HEIGHT};
  186.  
  187. // make the call to adjust window_rect
  188. AdjustWindowRectEx(&window_rect,
  189.      GetWindowStyle(main_window_handle),
  190.      GetMenu(main_window_handle) != NULL,
  191.      GetWindowExStyle(main_window_handle));
  192.  
  193. // save the global client offsets, they are needed in DDraw_Flip()
  194. window_client_x0 = -window_rect.left;
  195. window_client_y0 = -window_rect.top;
  196.  
  197. // now resize the window with a call to MoveWindow()
  198. MoveWindow(main_window_handle,
  199.            CW_USEDEFAULT, // x position
  200.            CW_USEDEFAULT, // y position
  201.            window_rect.right - window_rect.left, // width
  202.            window_rect.bottom - window_rect.top, // height
  203.            FALSE);
  204.  
  205. // show the window, so there's no garbage on first render
  206. ShowWindow(main_window_handle, SW_SHOW);
  207. } // end if windowed
  208.  
  209. // perform all game console specific initialization
  210. Game_Init();
  211.  
  212. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  213. // if it causes your system to crash
  214. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  215.  
  216. // enter main event loop
  217. while(1)
  218.     {
  219.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  220.         { 
  221.         // test if this is a quit
  222.         if (msg.message == WM_QUIT)
  223.            break;
  224.     
  225.         // translate any accelerator keys
  226.         TranslateMessage(&msg);
  227.  
  228.         // send the message to the window proc
  229.         DispatchMessage(&msg);
  230.         } // end if
  231.     
  232.     // main game processing goes here
  233.     Game_Main();
  234.  
  235.     } // end while
  236.  
  237. // shutdown game and release all resources
  238. Game_Shutdown();
  239.  
  240. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  241. // if it causes your system to crash
  242. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  243.  
  244. // return to Windows like this
  245. return(msg.wParam);
  246.  
  247. } // end WinMain
  248.  
  249. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  250.  
  251. int Game_Init(void *parms)
  252. {
  253. // this function is where you do all the initialization 
  254. // for your game
  255.  
  256. // start up DirectDraw (replace the parms as you desire)
  257. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  258.  
  259. // initialize directinput
  260. DInput_Init();
  261.  
  262. // acquire the keyboard 
  263. DInput_Init_Keyboard();
  264.  
  265. // add calls to acquire other directinput devices here...
  266.  
  267. // hide the mouse
  268. ShowCursor(FALSE);
  269.  
  270. // seed random number generator
  271. srand(Start_Clock());
  272.  
  273. // all your initialization code goes here...
  274.  
  275.  
  276. // return success
  277. return(1);
  278.  
  279. } // end Game_Init
  280.  
  281. ///////////////////////////////////////////////////////////
  282.  
  283. int Game_Shutdown(void *parms)
  284. {
  285. // this function is where you shutdown your game and
  286. // release all resources that you allocated
  287.  
  288. // shut everything down
  289.  
  290. // release all your resources created for the game here....
  291.  
  292.  
  293. // now directsound
  294. DSound_Stop_All_Sounds();
  295. DSound_Shutdown();
  296.  
  297. // shut down directinput
  298. DInput_Shutdown();
  299.  
  300. // shutdown directdraw last
  301. DDraw_Shutdown();
  302.  
  303. // return success
  304. return(1);
  305. } // end Game_Shutdown
  306.  
  307. //////////////////////////////////////////////////////////
  308.  
  309. int Game_Main(void *parms)
  310. {
  311. // this is the workhorse of your game it will be called
  312. // continuously in real-time this is like main() in C
  313. // all the calls for you game go here!
  314.  
  315. int index; // looping var
  316.  
  317. // start the timing clock
  318. Start_Clock();
  319.  
  320. // clear the drawing surface 
  321. DDraw_Fill_Surface(lpddsback, 0);
  322.  
  323. // read keyboard and other devices here
  324. DInput_Read_Keyboard();
  325.  
  326.  
  327. // game logic here...
  328.  
  329.  
  330. // flip the surfaces
  331. DDraw_Flip();
  332.  
  333. // sync to 30ish fps
  334. Wait_Clock(30);
  335.  
  336. // check of user is trying to exit
  337. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  338.     {
  339.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  340.  
  341.     } // end if
  342.  
  343. // return success
  344. return(1);
  345.  
  346. } // end Game_Main
  347.  
  348. //////////////////////////////////////////////////////////